package com.healer.springcloud.controller;

import com.healer.springcloud.pojo.Department;
import com.healer.springcloud.service.Impl.DepartmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//提供restful服务
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    DepartmentServiceImpl departmentService;
//    获取一些配置信息，得到具体的微服务
    @Autowired
    private DiscoveryClient client;

    @GetMapping("/adddepartment/{name}")
    public boolean addDept(@PathVariable("name") String name) {
        return departmentService.addDepartment(name);
    }

    @PostMapping("/querydepartmentbyid")
    public Department queryDepartmentById(@RequestBody Map<String,Object> map) {
        System.out.println("我曾经来过:"+map.get("id").toString());
        if(map==null) return new Department((long)0,"null","null");
        return departmentService.queryDepartmentById(Long.parseLong(map.get("id").toString()));
    }

    @GetMapping("/querydepartment")
    public List<Department> queryDepartment() {
        return departmentService.queryDepartment();
    }
//注册的微服务获取一些信息
    @GetMapping("/")
    public Object discovery(){
        List<String> services = client.getServices();
        System.out.println("discovery=>services:"+services);
//        得到具体的微服务，通过微服务id，applicationName
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPARTMENT");
        for (ServiceInstance i : instances){
            System.out.println(
                    i.getHost()+"\t"+
                    i.getPort()+"\t"+
                    i.getUri()+"\t"+
                    i.getServiceId()
            );
        }
        return this.client;
    }
}
